package com.cmas.platform.service.impl;

import java.util.List;

import com.aidex.common.annotation.DataSource;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.exception.BizException;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.cmas.platform.vo.SectionVo;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.platform.mapper.SectionMapper;
import com.cmas.platform.domain.Section;
import com.cmas.platform.service.SectionService;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.common.utils.NumberUtils;

/**
 * 版块管理Service业务层处理
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-14
 */
@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section> implements SectionService {

    private static final Logger log = LoggerFactory.getLogger(SectionServiceImpl.class);


    /**
     * 校验版块编码的唯一性
     *
     * @param section 版块
     */
    @Override
    public boolean checkCodeUnique(Section section) {
        QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
        Section notEqual = baseMapper.selectOne(queryWrapper
                .lambda().eq(Section::getCode, section.getCode()));
        if (notEqual != null) {
            return !notEqual.getId().equals(section.getId());
        }
        return false;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Section> selectSectionList(Section section) {
        QueryWrapper<Section> queryWrapper = setQueryWrapper(section);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询版块管理列表
     *
     * @param section 版块管理
     * @return 版块管理
     */
    @Override
    public List<SectionVo> selectSectionVoList(Section section) {
        return baseMapper.selectSectionList(setQueryWrapper(section));
    }

    /**
     * 设置QueryWrapper条件
     *
     * @param section
     * @return
     */
    public QueryWrapper<Section> setQueryWrapper(Section section) {
        QueryWrapper<Section> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(section.getRegionId())) {
            queryWrapper.eq("section.region_id", section.getRegionId());
            queryWrapper.or().eq("section.type", "0");
        }
        if (StringUtils.isNotEmpty(section.getCode())) {
            queryWrapper.eq("section.code", section.getCode());
        }
        if (StringUtils.isNotEmpty(section.getType())) {
            queryWrapper.eq("section.type", section.getType());
        }
        if (StringUtils.isNotEmpty(section.getName())) {
            queryWrapper.like("section.name", section.getName());
        }
        return queryWrapper;
    }

    /**
     * 批量删除版块管理
     *
     * @param ids 需要删除的版块管理ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteSectionByIds(String[] ids) {
        return baseMapper.deleteSectionByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    /**
     * 获取最大编号
     *
     * @param section 版块管理
     * @return 结果
     */
    @Override
    public int findMaxSort(Section section) {
        return NumberUtils.nextOrder(baseMapper.findMaxSort(section));
    }
}
